% Solve for equilibrium and  given parameters and generate all tables in the paper

%% Parameters
parameters

% load changes in transfers that induce devaluations
if monly~=1 && nafta~=1 && eta045NGF~=1 && eps22NGF~=1 && eps27NGF~=1 && eps22glob~=1 && eps27glob~=1 && eta045glob~=1 && eta35NGF~=1 && eta35glob~=1 
    param.dT = x(13); % T' = T - dT, 
end
if monly==1
    param.dT = 0.0095;
end
if nafta==1
   param.dT=0;
   param.dz = 0.9418; % lower input tariffs of 6 pp, i.e., Delta log (z) = -0.06.
   param.dbl = 1.13 ; % lower output export tariffs (Delta log (1+tau) = -0.048), implemented as an equivalent increase in bl (scale parameter of market size distribution across countries), as per Delta log (bl) = - (sg-1) Delta log (1+tau)
end
if eta35glob ==1 
    param.dT = 0.044;
end
if  eta35NGF ==1
    param.dT = 0.0561; 
end
if eta045glob==1
    param.dT = 0.0525;
end
if eta045NGF==1
    param.dT = 0.0485;
end
if eps27glob==1 
    param.dT = 0.04375;
end
if eps27NGF==1
    param.dT = 0.058;
end
if eps22glob==1 
    param.dT = 0.0575;
end
if eps22NGF==1 
    param.dT = 0.0565;
end

%% Distribution of efficiency and fixed costs of importing and exporting

% Standard normals for productivity and fixed costs
if monly==1
    load Heterogeneity_Importing.mat
else
    load Heterogeneity.mat
end

phi = DrawProductivity(U,param);
fc = DrawFixedCostsImporting(param,V,phi);
fx = DrawFixedCostsExporting(param,Z,phi,fc);

%% Target moments in data
m(1) = 0.36;m(2) = 0.16;m(3) = 0.26;m(4) = 0.07;m(5) = 0.18;m(6) = 1.71;m(7) = 0.27;m(8) = 0.18 ;m(9) = 0.27;m(10) = 0.19;m(11)= 0.15;
% Aggregate Import Share,Aggregate Export Share, Share Importers-only, Share Exporters-only, Share Exporters-Importers, Standard dev of log value added,Standard dev of import shares (level),Standard deviation export shares (level), Correlation log va - import shares (level), Correlation import shares- export shares, Correlation log va - export shares (level)

param.shareN = 0.3870; param.shareM = 10.6939/100;
% Expenditure share in non-tradables and final good import share

%% Optimal domestic shares and international strategy

optimalshares

%% Targeted moments in the model and statistics on distributions of sales, import and export shares

moments

%% Calibrate varrho and T

% Given distribution of import and export shares, find weight of imported
% final good in preferences (varrho), pre-devaluation wage (w) and transfers
% (T). See calibration appendix.

x0 = [0.1,0.2,0.15];
[w,~,~, varrho,Timplied] = calibrateaNM_byHand(sD, sX, phi,fc,fx,param); % Note: Expenditure share in non-tradables and final good import share are passed via param
param.varrho = varrho; param.T = Timplied;

%% Retrieve aggregate domestic spending (S), price index (P) and other equilibrium objects

[SP,SP2,P,RX,M,Iw, sN, sM,Ptot,LN] = GEobjects(w,sD, sX, phi,fc,fx, param); % SP,SP2,P are in tradables

disp('Expenditure share in non-tradables  Final good import share')
disp([sN sM])

MF= (1-sN)*sM * Iw; % final good imports in domestic labor
TotalExports_norm = TotalExports * (P/w)^((sg-1)*(1-g) ) * (SP/w) * knorm; % exports in domestic labor
TotalImports_norm = TotalImports * (P/w)^((sg-1)*(1-g) ) * (SP/w) * knorm + MF; % total imports in domestic labor
R = SP + RX*w; % total sales in tradables
zvec =z; yvec=y;
disp('Check balanced trade condition pre devaluation')   
disp(((TotalImports_norm)/(TotalExports_norm +param.T)-1)*100) % excess imports relative to exports (+ transfers) in percentage points; should be zero.

%% Retrieve fixed costs:
[fc_norm,fx_norm,FX_norm,FM_norm,FXM_norm] = NormalizedFC(fc, fx, FX,FM,FXM,SP,P,w,param);

%% Devaluation       
param.T = param.T - param.dT;
param.bl = param.bl * param.dbl;
param.z = param.z * param.dz;
               
[x_sol, MSE] = DevaluationFixedPoint(param, phi, fc_norm, fx_norm,FXM_norm,FX_norm,FM_norm, SDM,SDXM,z,y, P, SP,w,Opt_options);

Peq = abs(x_sol(1)); Seq = abs(x_sol(2)); weq = abs(x_sol(3));  

[PriceChange, Mn, RXn,EqMoments,sD1,omega1,sX1,TotalExports_norm1,TotalImports_norm1,SPn,In,wn,sNn,sMn,Ptotn,LNn] = DevaluationResults(param, phi, fc_norm, fx_norm,FXM_norm,FX_norm,FM_norm, SDM,SDXM,z,y, P,Peq,Seq,weq);
                                                            
postdevaluationresults


